import QtQuick 1.1
import com.nokia.meego 1.0
import "qrc:/wolmeup.js" as Wolmeupjs
import "qrc:/storage.js" as Storage

PageStackWindow {
    id: appWindow
    //
    // Signals and slots
    //
    signal macIncr(int macnum, int macdigit)
    signal macDecr(int macnum, int macdigit)
    signal updateMacs
    signal sendMacToCPP(int macnum)
    signal storeMACsToSQL
    property bool hasMACChanged: false;
    onMacDecr: {
        Wolmeupjs.macDecr(macnum, macdigit);
        appWindow.updateMacs();
        hasMACChanged = true;
    }
    onMacIncr: {
        Wolmeupjs.macIncr(macnum, macdigit);
        appWindow.updateMacs();
        hasMACChanged = true;
    }
    onUpdateMacs: { // get new MACs from JS
        tab1.mac = Wolmeupjs.macToStr(1);
        tab2.mac = Wolmeupjs.macToStr(2);
        tab3.mac = Wolmeupjs.macToStr(3);
    }
    onSendMacToCPP: {
        BroadCaster.broadCastMAC(Wolmeupjs.macToStr(macnum));
    }
    onStoreMACsToSQL: {
        Storage.setSetting("mac1",tab1.mac);
        Storage.setSetting("mac2",tab2.mac);
        Storage.setSetting("mac3",tab3.mac);
    }
    Connections{
        target: platformWindow
        onActiveChanged: {sqlTimer.running = platformWindow.active;}
    }

    Timer { // check every 2 seconds if macs have changed and store if so
             //interval: 2000; running: true; repeat: true
        id: sqlTimer
        interval: 2000; running: true; repeat: true
        onTriggered:{if(hasMACChanged) {appWindow.storeMACsToSQL(); hasMACChanged = false;}}
    }
    //
    // Read database entries to MACs
    //

    Component.onCompleted: {
        // Initialize the database
        Storage.initialize();
        tab1.mac = Storage.getSetting("mac1");
        tab2.mac = Storage.getSetting("mac2");
        tab3.mac = Storage.getSetting("mac3");
        Wolmeupjs.setMac(1, tab1.mac);
        Wolmeupjs.setMac(2, tab2.mac);
        Wolmeupjs.setMac(3, tab3.mac);
        theme.inverted = true;
    }

    //
    // Page itself
    //
    ToolBarLayout {
        id: tabBarLayout
        visible: true
        anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
        //ToolIcon { iconId: "toolbar-back"; onClicked: pageStack.pop(); }
        ButtonRow {
            style: TabButtonStyle { }
            TabButton {
                text: "MAC1"
                tab: tab1
            }
            TabButton {
                text: "MAC2"
                tab: tab2
            }
            TabButton {
                text: "MAC3"
                tab: tab3
            }

        } // end ButtonRow
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    } // end ToolBarLayout

    initialPage: blankPage
    Page{
        id: blankPage
        tools: tabBarLayout
        orientationLock: PageOrientation.LockPortrait

        TabGroup {
            id: tabGroup
            currentTab: tab1
            //anchors { left: parent.left; right: parent.right; bottom: tabBarLayout.top; top: parent.bottom }
            MainPage {
                id: tab1
                //mac: Wolmeupjs.macToStr(1);
                macTab: 1;
            }
            MainPage {
                id: tab2
                //mac: Wolmeupjs.macToStr(2);
                macTab: 2;
            }

            MainPage {
                id: tab3
                //mac: Wolmeupjs.macToStr(3);
                macTab: 3;
            }
        }
        Menu {
            id: myMenu
            visualParent: pageStack
            //platformStyle: MenuStyle
            MenuLayout {
                MenuItem {
                    text: qsTr("Reset MACs");
                    onClicked: {
                        Wolmeupjs.setMac(1, "000000000000");
                        Wolmeupjs.setMac(2, "000000000000");
                        Wolmeupjs.setMac(3, "000000000000");
                        updateMacs();
                        hasMACChanged = true;
                    }
                }
                MenuItem {
                    text: qsTr("About");
                    onClicked:{pageStack.push(Qt.resolvedUrl("WolHelpPage.qml"))}
                }
            }
        } // end Menu
    }
}
